﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HYI.SDBSERVER;

namespace SHRBIZE
{
    public partial class FMI_TC_GOODINV : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sSite_No;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private int iGridView2 = 0;

        public FMI_TC_GOODINV()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string sState = "0";
            DataRowView dr1 = (DataRowView)uiCbState.SelectedItem;
            if ((dr1 != null) && (uiCbState.SelectedIndex > 0))
            {
                sState =dr1.Row["fdictno"].ToString();
            }

            string sCabNo = uicabbarcode.Text;
            if (uicabbarcode.Text != "")
            {
                sCabNo = uicabbarcode.Tag.ToString();
            }

            string sBoxNo = uiboxbarcode.Text;
            if (uiboxbarcode.Text != "")
            {
                sBoxNo = uiboxbarcode.Tag.ToString();
            }

            string SQL = "exec proc_cab_dogetgoodinv "
                + cbtype.SelectedIndex.ToString()
                +",'"+ sCabNo+ "'"
                +",'"+ sBoxNo + "'"
                +",'"+uirfbarcode.Text+"'"
                +",'"+uibatchano.Text+"'"
                +",'"+sState+ "'"
                +",'"+sUser+"'";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
        }

        private void FMI_TC_GOODINV_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys10.Visible = true; sys10.Text = "库存明细";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            cbtype.SelectedIndex = 1;

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=3005");

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            uiCbState.DataSource = myTs_Dict;
            uiCbState.DisplayMember = "fDictName";
            uiCbState.ValueMember = "fDictNo";
            uiCbState.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //查看库存明细
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            uifcabname.Text = uiDGView["fcabname", rowindex].Value.ToString();
            uifequipmenttag.Text = uiDGView["fequipmenttag", rowindex].Value.ToString();
            uifbatch.Text = uiDGView["fbatch", rowindex].Value.ToString();
            uifrfidbarcode.Text = uiDGView["frfidbarcode", rowindex].Value.ToString();
            uifgoodno.Text = uiDGView["fgoodno", rowindex].Value.ToString();
            uifgoodname.Text = uiDGView["fgoodname", rowindex].Value.ToString();

            string sql = "select convert(varchar(20),a.fupddate,120) fupddate,c.username,b.fdictname "
             + " ,a.fqty * case a.fbizType when 5 then -1 else 1 end fqty "
             + "  from i_warehouse_dtl a,s_ts_dict b, am_sys_user c"
             + "  where a.fbatch = '" + uifbatch.Text + "'"
             + "  and b.fdicttype = 3004 and b.fdictno = cast(a.fbizType as varchar)"
             + "  and b.fdictno not in ('2','3')"
             + "  and a.foperator = cast(c.id as varchar)"
             + "  order by a.fupddate";
            DataTable myDetail = StaticClass.myDB.query(sql);
            uiDetail.DataSource = myDetail;

            base.OnSys10();

            //表格列显示统一控制
            if (iGridView2 == 0)
            {
                myCache.doSetGridVIew(sMenu + "1", sUser, uiDetail);
                iGridView2 = iGridView2 + 1;
            }
        }

        private void uiDetail_DoubleClick(object sender, EventArgs e)
        {
            string iMenu = sMenu + "1";
            OnGridDbClick(iMenu, uiDetail);
        }

        private void uiCbState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbtype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uiBtn1_Click(object sender, EventArgs e)
        {
            //弹窗选择储物柜
            uicabbarcode.Text = "";
            uicabbarcode.Tag = "";

            string DATA = doPOPWINDOS("储物柜查询"
                , "fequipmentbarcode,储物柜编号,121;fequipmenttag,储物柜名称,141"
                , "select fequipmentbarcode,fequipmenttag,fmacno from i_ts_cabinet where 1=1"
                , " order by fequipmentbarcode"
                , 0);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strArray = DATA.Split(',');

            uicabbarcode.Text = strArray[1];
            uicabbarcode.Tag = strArray[0];
        }

        private void uiBtn2_Click(object sender, EventArgs e)
        {
            //弹窗选择储物仓
            uiboxbarcode.Text = "";
            uiboxbarcode.Tag = "";

            string DATA = doPOPWINDOS("储物仓查询"
                , "fequipmentbarcode,储物仓编号,121;fequipmenttag,储物仓名称,141"
                , "select fequipmentbarcode,fequipmenttag,fmacno from i_ts_warehouse where 1=1"
                , " order by fequipmentbarcode"
                , 0);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strArray = DATA.Split(',');

            uiboxbarcode.Text = strArray[1];
            uiboxbarcode.Tag = strArray[0];
        }
    }
}
